package com.bird.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;

import weibo4j.org.json.JSONObject;


public class HttpClientUtils {
	
	
	public String SendGET(String keywords) {
		
		String urlPath = "http://www.simsimi.com/talk.htm?lc=ch";
		URL urlCookie;
		String cookie = null;
		try {
			urlCookie = new URL(urlPath);
			HttpURLConnection conn = (HttpURLConnection) urlCookie.openConnection();
			conn.setRequestMethod("GET");
			conn.setReadTimeout(10000);  // time out in 10 seconds
			cookie = conn.getHeaderField(5);
			cookie = cookie.substring(0,cookie.indexOf(";"));
			cookie = cookie+";";
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
	//	Log.log("keywords "+keywords);
		
		String url = "http://www.simsimi.com/func/req?msg="+keywords+"&lc=ch";
		url = url.replaceAll(" ", "%20");
		
		HttpClient httpclient = HttpConnectionManager.getHttpClient();
		InputStream in = null;
		InputStreamReader reader = null;
		BufferedReader buff = null;
		HttpGet httpget = null;
		try {
		    httpget = new HttpGet(url);
		    httpget.setHeader("cookie", cookie);
			httpget.setHeader("User-Agent",
					"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
			httpget.setHeader("REFERER", "http://www.simsimi.com/talk.htm?lc=ch");
			// System.out.println("executing request " + httpget.getURI());

			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();

			if (response.getStatusLine().getStatusCode() != 200) {
				throw new RuntimeException();
			}

			if (entity != null) {
				in = entity.getContent();
			    reader = new InputStreamReader(in,"UTF-8");
			    buff = new BufferedReader(reader);
			    String temp = null;
			    StringBuilder sb = new StringBuilder();
			    while((temp=buff.readLine())!=null){
			    	sb.append(temp);
			    }
			    
			    JSONObject json = new JSONObject(sb.toString());
				return String.valueOf(json.get("response"));
			}
			// httpget.abort();
		} catch (Exception e) {
			httpget.abort();
			throw new RuntimeException(e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
			
			
			if (reader != null) {
				try {
					reader.close();
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
			
			
			if (buff != null) {
				try {
					buff.close();
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
		}
		return null;
	}
	
	
	
	public void getToken() {
	
		String url = "http://www.simsimi.com/func/req?msg=&lc=ch";
		url = url.replaceAll(" ", "%20");
		
		HttpClient httpclient = HttpConnectionManager.getHttpClient();
		InputStream in = null;
		InputStreamReader reader = null;
		BufferedReader buff = null;
		HttpGet httpget = null;
		try {
		    httpget = new HttpGet(url);
		   
			httpget.setHeader("User-Agent",
					"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)");
	//		httpget.setHeader("REFERER", "http://www.simsimi.com/talk.htm?lc=ch");
			
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();

			if (response.getStatusLine().getStatusCode() != 200) {
				throw new RuntimeException();
			}

			if (entity != null) {
				in = entity.getContent();
			    reader = new InputStreamReader(in,"UTF-8");
			    buff = new BufferedReader(reader);
			    String temp = null;
			    StringBuilder sb = new StringBuilder();
			    while((temp=buff.readLine())!=null){
			    	sb.append(temp);
			    }
			    
			    JSONObject json = new JSONObject(sb.toString());
				System.out.println(sb.toString());
			}
			// httpget.abort();
		} catch (Exception e) {
			httpget.abort();
			throw new RuntimeException(e);
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
			
			
			if (reader != null) {
				try {
					reader.close();
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
			
			
			if (buff != null) {
				try {
					buff.close();
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
		}
		
	}
}
